﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.UserControls;

    public class AlternateSkuList : BasePage
    {
        protected Button btnReSearch;
        protected HtmlForm form1;
        protected GridView gvAlternateSku;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected Label lblAlternateSkuList;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindDataAlternateSkuList(AlternateSkuQueryEntity alternateSkuQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(alternateSkuQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = BaseInfoDetailServiceClientProxy.GetInstance().GetDetaiInfolByQueryEntity(alternateSkuQuery, "ALTERNATE_SKU");
            base.BindData2GridView(this.gvAlternateSku, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            AlternateSkuQueryEntity alternateSkuQuery = this.GetAlternateSkuQueryEntity();
            alternateSkuQuery.CurrentPage = "0";
            this.BindDataAlternateSkuList(alternateSkuQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            AlternateSkuQueryEntity alternateSkuQuery = this.GetAlternateSkuQueryEntity();
            alternateSkuQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDataAlternateSkuList(alternateSkuQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            AlternateSkuQueryEntity alternateSkuQuery = this.GetAlternateSkuQueryEntity();
            if (int.Parse(alternateSkuQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                alternateSkuQuery.CurrentPage = (int.Parse(alternateSkuQuery.CurrentPage) + 1).ToString();
                this.BindDataAlternateSkuList(alternateSkuQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            AlternateSkuQueryEntity alternateSkuQuery = this.GetAlternateSkuQueryEntity();
            if (int.Parse(alternateSkuQuery.CurrentPage) > 0)
            {
                alternateSkuQuery.CurrentPage = (int.Parse(alternateSkuQuery.CurrentPage) - 1).ToString();
                this.BindDataAlternateSkuList(alternateSkuQuery);
            }
        }

        private AlternateSkuQueryEntity GetAlternateSkuQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as AlternateSkuQueryEntity);
            }
            return (base.PageQueryEntitySession as AlternateSkuQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                AlternateSkuQueryEntity alternateSkuQuery = this.GetAlternateSkuQueryEntity();
                alternateSkuQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDataAlternateSkuList(alternateSkuQuery);
            }
        }

        protected void gvAlternateSku_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
            }
        }

        protected void gvAlternateSku_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string selectData = string.Empty;
            selectData = (base.UrlReplace(this.gvAlternateSku.DataKeys[e.NewSelectedIndex]["SKU_ID"].ToString()) + "," + base.UrlReplace(this.gvAlternateSku.DataKeys[e.NewSelectedIndex]["ALT_SKU_ID"].ToString())) + "," + base.UrlReplace(this.gvAlternateSku.DataKeys[e.NewSelectedIndex]["VENDOR_ID"].ToString());
            if (!string.IsNullOrEmpty(selectData))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + selectData + "';  window.close();</script>", false);
            }
        }

        protected void gvAlternateSku_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                AlternateSkuQueryEntity alternateSkuQuery = this.GetAlternateSkuQueryEntity();
                alternateSkuQuery.SortField = e.SortExpression;
                if (alternateSkuQuery.SortDirection == "ASC")
                {
                    alternateSkuQuery.SortDirection = "DESC";
                }
                else if (alternateSkuQuery.SortDirection == "DESC")
                {
                    alternateSkuQuery.SortDirection = "ASC";
                }
                this.BindDataAlternateSkuList(alternateSkuQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            AlternateSkuQueryEntity alternateSkuQuery = new AlternateSkuQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    alternateSkuQuery.AltSkuID = this.PreviousPage.AltSkuID;
                    alternateSkuQuery.OwnerID = this.PreviousPage.OwnerID;
                    alternateSkuQuery.SkuID = this.PreviousPage.SkuID;
                    alternateSkuQuery.VendorID = this.PreviousPage.VendorID;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"];
                alternateSkuQuery.OwnerID = this.PreviousPage.OwnerID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"];
            }
            alternateSkuQuery.IsGetAll = false;
            alternateSkuQuery.CurrentPage = "0";
            alternateSkuQuery.PageSize = this.gvAlternateSku.PageSize.ToString();
            alternateSkuQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvAlternateSku.PageSize;
            this.SetAlternateSkuQueryEntity(alternateSkuQuery);
            this.BindDataAlternateSkuList(alternateSkuQuery);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetAlternateSkuQueryEntity(AlternateSkuQueryEntity alternateSkuQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = alternateSkuQuery;
            }
            else
            {
                base.PageQueryEntitySession = alternateSkuQuery;
            }
        }

        public AlternateSkuQuery PreviousPage
        {
            get
            {
                return (AlternateSkuQuery) base.PreviousPage;
            }
        }
    }
}

